package com.sdry.web.controller;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import com.sdry.entity.PartReturn;
import com.sdry.entity.PartReturnDetail;
import com.sdry.entity.PartReturnDetailExample;
import com.sdry.entity.PartReturnExample;
import com.sdry.service.PartReturnDetailService;
import com.sdry.service.PartReturnService;
import com.sdry.utils.RequestResult;
import com.sdry.utils.TableResult;

/**
 * 
* @Title: partReturnController.java
* @Package com.sdry.web.controller
* @Description: 退货
* @author cb
* @date 2018年8月1日
* @version V1.0
 */
@Controller
@RequestMapping("/partReturn")
public class PartReturnController {
	
	@Resource
	private PartReturnService partReturnService;
	
	@Resource
	private PartReturnDetailService partReturnDetailService;
	

	@RequestMapping("/pagePartReturn")
	public String pagePartReturn() {
		
		return "return/list";
	}
	
	/**
	 * 分页
	 * @param limit 大小
	 * @param offset 当前页
	 * @return
	 */
	@RequestMapping("/list")
	@ResponseBody
	public TableResult list(@RequestParam(value="limit",required=false)String limit,@RequestParam(value="offset",required=false)String offset) {

    	PageHelper.startPage(Integer.parseInt(offset), Integer.parseInt(limit));
    	
		PartReturnExample example = new PartReturnExample();
		List<PartReturn> list = partReturnService.selectByExample(example);
    	
    	PageInfo<PartReturn> info = new PageInfo<PartReturn>(list);
    	
    	TableResult tableResult = new TableResult(info.getTotal(), info.getList()); 
    	
		return tableResult;
    	
	}
	
	/**
	 * 添加
	 * @param record
	 * @return
	 */
	@RequestMapping("/insert")
	@ResponseBody
	public Map<String, Object> insert(PartReturn record,HttpServletRequest request ,@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:sss")Date time) {
		String condes = request.getParameter("condes");
		
		String[] split = condes.split(",");
		
		
		DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");//设置日期格式
		String newsNo = "tk"+LocalDateTime.now().format(fmt);
		
		record.setPremark1(newsNo);
		
		record.setPdate(time);
		
		PartReturnDetail detail = new PartReturnDetail();
		detail.setRemrk1(newsNo);
		
		for (String conde : split) {
			
			detail.setCode(conde);
			
			partReturnDetailService.insertSelective(detail);
		}
		
		
		
		int effect = partReturnService.insertSelective(record);
		
	    return RequestResult.requestResultLong(effect);
	}
	
	
	/**
	 * 根据ID查询
	 * @param id
	 */
	@RequestMapping("/selectById")
	@ResponseBody
	public PartReturn selectById(long id) {
		return partReturnService.selectById(id);
	}
	
	/**
	 * 删除
	 * @param ids 主键数组
	 * @return
	 */
	@RequestMapping("/delete")
	@ResponseBody
	public Map<String, Object> delete(String ids) {
		
		int effect = 0;
		
		String[] split = ids.split(",");
		for (String str : split) {
			effect = partReturnService.delete(Long.parseLong(str));
		}
		
		return RequestResult.requestResultLong(effect);
		
	}
	
	/**
	 * 查询详细信息
	 * @param remark1 编号
	 * @return
	 */
	@RequestMapping("/listDetail")
	@ResponseBody
	public TableResult listDetail(String remark1) {
		
		if (StringUtil.isEmpty(remark1)) {
			
			TableResult result = new TableResult();
			
			return result;
		}
		
		PartReturnDetailExample example = new PartReturnDetailExample();
		
		example.createCriteria().andRemrk1EqualTo(remark1);
		List<PartReturnDetail> list = partReturnDetailService.selectByExample(example);
		
		TableResult tableResult = new TableResult(null, list); 
    	
		return tableResult;
		
	}
	
	
	
}
